package com.lkc.controller;

import com.lkc.util.ImageUtil;
import com.lkc.util.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class UploadController {
    @RequestMapping("/file")
    public Result upload(@RequestParam("file") MultipartFile  file) {
        try {
            InputStream is=file.getInputStream();
            String filename = file.getOriginalFilename();
            String newFileName = ImageUtil.upload(filename,is);
            return Result.success("图片上传成功",newFileName);
        } catch (IOException e) {
            throw new RuntimeException("图片上传失败");
        }

    }
}
